package UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JToolBar;

/**
 *
 * @author Christophe
 */
public class MainPanel extends JPanel {

    /*
     * Creation for a Borderlayout for the different Panels which were added later
     * on
     */
    private BorderLayout myBorderLayout = new BorderLayout();
    private String mode = "";
    private DefaultListModel listModel = new DefaultListModel();
    private JList lstGates = new JList();
    private DrawPanel dpnl0 = new DrawPanel(listModel);
    /*
     * Construction of the MainPanel
     */

    public MainPanel() {
        setLayout(myBorderLayout);
        JToolBar myToolBar = new JToolBar("Logic Gate");
        addButtons(myToolBar);
        add(myToolBar, BorderLayout.PAGE_START);
        setDoubleBuffered(true);
        
        add(dpnl0, BorderLayout.CENTER);
    }

    /*
     * Get the mode to say which logical gate should be drawn
     */
    public String getMode() {
        return mode;
    }

    /*
     * Set the mode to declare which logical gate should be drawn
     */
    public void setMode(String mode) {
        this.mode = mode;
    }

    /*
     * Add the buttons to the toolbar
     */
    public final void addButtons(JToolBar toolBar) {
        JButton button = null;

        button = makeChoiseButton("And", "AND", "Logical And", "And");
        toolBar.add(button);

        button = makeChoiseButton("Nand", "NAND", "Logical Nand", "NAnd");
        toolBar.add(button);

        button = makeChoiseButton("Or", "OR", "Logical Or", "Or");
        toolBar.add(button);

        button = makeChoiseButton("Nor", "NOR", "Logical Nor", "Nor");
        toolBar.add(button);

        button = makeChoiseButton("Not", "NOT", "Logical Not", "Not");
        toolBar.add(button);

        button = makeChoiseButton("Xor", "XOR", "Logical Xor", "Xor");
        toolBar.add(button);

        button = makeChoiseButton("NXor", "NXOR", "Logical Nxor", "Nxor");
        toolBar.add(button);
        
        button = makeChoiseButton("Switch", "SWITCH", "On and off switch", "Switch");
        toolBar.add(button);
    }

    /*
     * Create the buttons ´for the toolbar
     */
    protected JButton makeChoiseButton(String imageName,
            String actionCommand,
            String toolTipText,
            String altText) {
        String imgLocation = "./pics/" + imageName + ".png";
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(imgLocation));
        } catch (IOException e) {
            System.out.println("Image was not found");
        }

        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(new changeMode());

        Icon myIcon = new ImageIcon(img);
        button.setIcon(myIcon);

        return button;
    }

    /*
     * Paints the grid
     */
//    @Override
//    protected void paintComponent(Graphics g) {
//        super.paintComponent(g);
//        g.setColor(Color.white);
//        g.fillRect(0, 0, getWidth(), getHeight());
//        g.setColor(Color.lightGray);
//        int dx = 25;
//        int dy = 25;
//        for (int i = 0; i < getWidth(); i++) {
//            g.drawLine((int) (i * dx), 0, (int) (i * dx), getHeight());
//        }
//        for (int i = 0; i < getHeight(); i++) {
//            g.drawLine(0, (int) (i * dx), getWidth(), (int) (i * dx));
//        }
//    }

    /*
     * Action to change the mode
     */
    public class changeMode implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("AND")) {
                dpnl0.setCurrentGateType(0);
            } else if (e.getActionCommand().equals("NAND")) {
                dpnl0.setCurrentGateType(5);
            } else if (e.getActionCommand().equals("OR")) {
                dpnl0.setCurrentGateType(1);
            } else if (e.getActionCommand().equals("NOR")) {
                dpnl0.setCurrentGateType(6);
            } else if (e.getActionCommand().equals("NOT")) {
                dpnl0.setCurrentGateType(2);
            } else if (e.getActionCommand().equals("XOR")) {
                dpnl0.setCurrentGateType(3);
            } else if (e.getActionCommand().equals("NXOR")) {
                dpnl0.setCurrentGateType(4);
            } else if (e.getActionCommand().equals("SWITCH")) {
                dpnl0.setCurrentGateType(7);
            }
        }
    }
}